package com.yc.anno;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class ClassDemo {
    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        A a=new A();
        A b=new B();

        //不通过反射调这个方法，看报错
        a.callTest("张三");
        shoObject(a);
        System.out.println("====================");
        shoObject(b);
    }
    public  static void  shoObject(Object object) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<?> aClass=object.getClass();
        //带路径的
        System.out.println("aClass.getName() = " + aClass.getName());
        //不带路径的
        System.out.println("aClass.getSimpleName() = " + aClass.getSimpleName());

        for (Field field : aClass.getFields()) {
            System.out.println("field = " + field);
        }
        for (Field declaredField : aClass.getDeclaredFields()) {
            System.out.println("declaredField = " + declaredField);
        }
        Method test=aClass.getDeclaredMethod("test",String.class);
        if(test!=null){
            //设置强制访问
            test.setAccessible(true);
            test.invoke(object,"张三");
        }
    }

}
class A{
    private int a;
    String b;
    protected Byte c;
    public Date d;
    private void test(int num){

    }
    private void test(String s){
        int i=1/0;
        System.out.println("hello "+s);

    }
    public void callTest(String s){
        test(s);
    }
}
class B extends A{
    @Override
    public void callTest(String s) {
        super.callTest(s);
    }
}
